﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace 音乐工具
{
    public class HttpHelper
    {
        public static string Post(string Url, string param, string proxy)
        {
            string message = string.Empty;
            HttpWebRequest request;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(Url);
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)";
                request.KeepAlive = true;
                request.Method = "POST";
                request.ProtocolVersion = HttpVersion.Version11;
                ServicePointManager.Expect100Continue = false;
                if (null != proxy && proxy != "")
                {

                    WebProxy t_proxy = new WebProxy(proxy);
                    request.Proxy = t_proxy;
                }
                request.ContentType = "application/x-www-form-urlencoded";
                byte[] bytes = Encoding.GetEncoding("utf-8").GetBytes(param);
                request.ContentLength = bytes.Length;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
                message = reader.ReadToEnd();
                reader.Close();
                responseStream.Close();
            }
            catch (Exception exception)
            {
                message = exception.Message;
            }
            return message;
        }

        public static string Get(string url, string ua)
        {
            string message = string.Empty;
            HttpWebRequest request;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.UserAgent = ua;
                if ("".Equals(ua.Trim())) request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)";
                request.KeepAlive = true;
                request.ProtocolVersion = HttpVersion.Version11;
                ServicePointManager.Expect100Continue = false;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
                message = reader.ReadToEnd();
                reader.Close();
                responseStream.Close();
            }
            catch (Exception exception)
            {
                message = exception.Message;
            }
            return message;
        }

        public static string Get(string url)
        {
            string message = string.Empty;
            HttpWebRequest request;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)";
                request.KeepAlive = true;
                request.ProtocolVersion = HttpVersion.Version11;
                ServicePointManager.Expect100Continue = false;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
                message = reader.ReadToEnd();
                reader.Close();
                responseStream.Close();
            }
            catch (Exception exception)
            {
                message = exception.Message;
            }
            return message;
        }
    }
}
